﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace WmsApp.UI
{
    public static class CheckQuerierItemFactory
    {
        public static ICheckQuerierItem Create(string text, ICheckQuerier container)
        {
            return new CheckQuerierItem(text,container);
        }
    }

    public interface ICheckQuerierItem
    {
        object Value { get;  }
        string Text { get; }
        bool Checked { get; }
        void SetEnable(bool b);
        int Left { get; }
        int CheckEditWidth { get; }
        void Clear();
        void Add(ICheckQuerierItem prevCheckQuerierItem);
    }

    public class CheckQuerierItem : ICheckQuerierItem
    {
        public string Text { get { return checkEdit.Properties.Caption; } }
        public object Value { get;private set; }
        public bool Checked { get; private set; }
        private CheckEdit checkEdit;
        private ICheckQuerier container;
        public CheckQuerierItem(string text, ICheckQuerier container)
        {
            this.container = container;
            checkEdit = new CheckEdit();
            Value = text;
            checkEdit.Text =  text  ;
            checkEdit.AutoSize = true;
            checkEdit.Enabled = false;
            checkEdit.CheckStateChanged += (o, e) => { Checked = checkEdit.Checked; };
        }

        public int Left
        {
            get { return this.checkEdit.Left; }
        }

        public int CheckEditWidth
        {
            get { return this.checkEdit.Width; }
        }

        public void SetEnable(bool b)
        {
            checkEdit.Enabled = b;
        }

        public void Clear()
        {
            if (container.Contains(checkEdit))
            {
                container.Controls.Remove(checkEdit);
            }
        }

        public void Add(ICheckQuerierItem prevCheckQuerierItem)
        {
            if (!container.Contains(checkEdit))
            {
                checkEdit.Properties.AutoWidth = true;
                container.Controls.Add(checkEdit);
                checkEdit.Top = 1;
                if (prevCheckQuerierItem != null)
                {
                    checkEdit.Left = 20 + prevCheckQuerierItem.Left + prevCheckQuerierItem.CheckEditWidth;
                }
                else
                {
                    checkEdit.Left = container.PrevWidth + 20;
                }
                checkEdit.Show();
            }
        }
    }
}
